package com.wing.storage.Controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.response.ProductVO;
import com.wing.storage.model.entity.ProductInventory;
import com.wing.storage.service.ProductInventoryService;
import com.wing.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 库存产品管理-Controller
 *
 * @author xiayang
 */
@Validated
@Api(value = "库存产品管理", tags = "#/storage/productInventory")
@RestController("productInventoryController")
@RequestMapping("storage/admin/productInventory")
@RequiredArgsConstructor
public class ProductInventoryController {

    @Autowired
    private ProductInventoryService productInventoryService;

    @ApiOperation(value = "库存产品列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductInventory> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "desc") String order,
            @RequestParam(name = "productTypeId", required = false) Long productTypeId,
            @RequestParam(name = "skuCode", required = false) String skuCode,
            @RequestParam(name = "skuName", required = false) String skuName,
            @RequestParam(name = "keyWord", required = false) String keyWord,
            @RequestParam(name = "skuSpec", required = false) String skuSpec,
            @RequestParam(name = "warehouseId", required = false) Long warehouseId,
            @RequestParam(name = "warehouseShelfId", required = false) Long warehouseShelfId,
            @RequestParam(name = "warehouseShelfSpaceId", required = false) Long warehouseShelfSpaceId,
            String startCreateTime,
            String endCreateTime,
            String warehouseShelfSpaceCode
    ) {
        Page<ProductInventory> pg = new Page<>(page, limit);
        Map<String, Object> map = new HashMap();
        map.put("productTypeId", productTypeId);
        map.put("skuCode", skuCode);
        map.put("keyWord", keyWord);
        map.put("skuName", skuName);
        map.put("skuSpec", skuSpec);
        map.put("warehouseName", warehouseId);
        map.put("warehouseShelfName", warehouseShelfId);
        map.put("warehouseShelfSpaceName", warehouseShelfSpaceId);
        map.put("startCreateTime", startCreateTime);
        map.put("endCreateTime", endCreateTime);
        map.put("warehouseShelfSpaceCode", warehouseShelfSpaceCode);
        map.put("sort", sort);
        map.put("order", order);
        IPage<ProductInventory> list = productInventoryService.pageList(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "畅捷通-现存量同步", notes = "畅捷通-现存量同步")
    @GetMapping("/sync")
    public JsonResult sync() {
        try {
            productInventoryService.chanjetCurrentStockSync();
            return JsonResult.success("畅捷通-现存量同步成功");
        }catch (BusinessException e){
            return JsonResult.fail(e.getMessage());
        }
    }



    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(name = "productTypeId", required = false) String productTypeId,
                       @RequestParam(name = "skuCode", required = false) String skuCode,
                       @RequestParam(name = "skuName", required = false) String skuName,
                       @RequestParam(name = "keyWord", required = false) String keyWord){
        List<ProductVO> list = new ArrayList<>();
        Map params = new HashMap();
        if(StringUtils.isNotBlank(keyWord)){
            params.put("keyWord", keyWord);
        }
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode", skuCode);
        }

        if(StringUtils.isNotBlank(keyWord)){
            params.put("skuName", skuName);
        }

        params.put("productTypeId",productTypeId);

        list = productInventoryService.selProductInventory(params);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        // 生成带时间戳的文件名
        String fileName = "库存导出_" + timestamp;
        ExcelUtils.export(response,fileName,list, ProductVO.class);
    }


}
